package com.t2c.tibetantochinese.fivehundred;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.t2c.tibetantochinese.R;
import com.t2c.tibetantochinese.base.BaseFragment;
import com.t2c.tibetantochinese.common.HttpRequest;
import com.t2c.tibetantochinese.common.HttpRequestManager;
import com.t2c.tibetantochinese.home.HomeFragment;
import com.t2c.tibetantochinese.moudle.FiveHundredList;
import com.t2c.tibetantochinese.realmModel.Category;
import com.t2c.tibetantochinese.search.SearchAdapterSimple;
import com.t2c.tibetantochinese.search.SearchResultFragment;
import com.t2c.tibetantochinese.utils.CommonUtil;
import com.t2c.tibetantochinese.utils.RealmUtil;
import com.t2c.tibetantochinese.views.TitleBar;

import java.util.List;

import io.realm.RealmResults;

/**
 * Created by zoulu on 16/6/20.
 * 汉藏500句
 */
public class FiveHundred extends BaseFragment{
    private ListView listView;
    private HomeFragment homeFragment;
    private RealmResults<Category> list;
    private RelativeLayout re_head;//搜索框
    private RelativeLayout re_search;//搜索框样式
    private EditText edittext;//搜索输入框
    private TextView tv_search;//搜索提示
    @Override
    protected int getContentViewResId() {
        return R.layout.fivehundred_layout;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init(view);
        initData();
    }

    private void init(View view){
        homeFragment = CommonUtil.getHomeFragment(getActivity());
        TitleBar titleBar = (TitleBar) view.findViewById(R.id.titlebar);
        re_head = (RelativeLayout) view.findViewById(R.id.re_head);
        TextView cancel = (TextView) view.findViewById(R.id.cancel);
        re_search = (RelativeLayout) view.findViewById(R.id.re_search);
        edittext = (EditText) view.findViewById(R.id.edittext);
        tv_search = (TextView) view.findViewById(R.id.tv_search);
        if(CommonUtil.isChinese(getActivity())) {
            titleBar.setTitle(getResources().getString(R.string.fivehundred));
            tv_search.setText(getResources().getString(R.string.search));
        }else{
            titleBar.getTitle().setTypeface(CommonUtil.getTibetanType(getActivity()));
            tv_search.setTypeface(CommonUtil.getTibetanType(getActivity()));
            titleBar.setTitle(getResources().getString(R.string.tfivehundred));
            tv_search.setText(getResources().getString(R.string.tsearch));
        }
        titleBar.setOnLeftListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                homeFragment.fragmentBack();
            }
        });
        listView = (ListView) view.findViewById(R.id.listview);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Fragment fragment = new FiveHundredDetailFragment();
                Bundle bundle = new Bundle();
                bundle.putInt("visiable",View.GONE);
                bundle.putInt("POS",list.get(position).getUuid());
                fragment.setArguments(bundle);
                homeFragment.switchContent(fragment);
                edittext.setText("");
            }
        });

        view.findViewById(R.id.re_search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                re_search.setVisibility(View.GONE);
                re_head.setVisibility(View.VISIBLE);
                edittext.requestFocus();
                CommonUtil.showKeyBoard(getActivity());
            }
        });
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                re_head.setVisibility(View.GONE);
                re_search.setVisibility(View.VISIBLE);
                CommonUtil.hideKeyboard(getActivity());
                edittext.setText("");
                initData();
            }
        });

        edittext.setOnEditorActionListener(new TextView.OnEditorActionListener(){
            @Override
            public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
                if(arg1 == EditorInfo.IME_ACTION_SEARCH)
                {
                    if(TextUtils.isEmpty(edittext.getText().toString())){
                        Toast.makeText(getActivity(),"请输入要搜索的内容",Toast.LENGTH_SHORT).show();
                    }else{
                        list = RealmUtil.getCategoryByName(edittext.getText().toString());
                        FiveHundredAdapter fiveHundredAdapter = new FiveHundredAdapter(getActivity(),list);
                        listView.setAdapter(fiveHundredAdapter);
                        CommonUtil.hideKeyboard(getActivity());
                    }
                }
                return false;
            }

        });
    }

    private void initData(){
        list = RealmUtil.getAllCategory();
        FiveHundredAdapter fiveHundredAdapter = new FiveHundredAdapter(getActivity(),list);
        listView.setAdapter(fiveHundredAdapter);
    }
}
